<template>
  <y-base-page title="加载中" theme="primary">
    <view class="main-page">
      <view class="loading">
        <text></text>
        <text></text>
        <text></text>
        <text></text>
        <text></text>
      </view>
      <view class="loadingText"> 正在努力加载... </view>
    </view>
  </y-base-page>
</template>

<script>
import { getSceneData } from "@/api/order.js";
import util from "@/lib/util.js";
export default {
  data() {
    return {
      scene: "",
      userInfo: null,
    };
  },
  async onLoad(option) {
    if (!option.scene && !uni.getStorageSync("scene")) {
      uni.reLaunch({
        url: "../home/home",
      });
      return;
    }

    if (option.scene) {
      this.scene = option.scene || uni.getStorageSync("scene");
      uni.setStorage({
        key: "scene",
        data: this.scene,
      });
      let requestHeader = uni.getStorageSync("requestHeader");

				let res = await getSceneData(option.scene);
				if (res.data.code && res.data.code == 200 ) {
					let data = res.data.data.substring(6);
					let arr = data.split('?');
					// 判断是否有登录，没有则跳转到登录页
					if (!requestHeader.Authorization && arr[0] != 'cbhEmployeeDetail') {
						this.handleToken()
					} else {
						this.getSceneData();
					}
				}else {
					let msg = "页面异常!"
					if (res.data.msg) {
						msg = res.data.msg
					}
					uni.showToast({
						title: msg,
						icon: "none"
					});
				}
			}
		},
		onShow() {
			this.userInfo = uni.getStorageSync('userInfo')
		},
		methods: {
			handleToken() {
				uni.showToast({
					title: '还没登录，请先登录',
					icon: 'none',
					duration: 2000
				})
				setTimeout(() => {
					uni.reLaunch({
						url: `../login/login?targetUrl=${encodeURIComponent(util.getCurrentPageUrlWithArgs())}`
					})
				}, 2000);
			},
			async getSceneData() {
				this.scene = uni.getStorageSync('scene');
				let res = await getSceneData(this.scene);
        		console.log('res:',res);
				let data = res.data.data.substring(6);
				let arr = data.split('?');
				const paramsArr = arr[1].split('&');
				let paramsObj = {};
				paramsArr.forEach(item => {
					if (/=/.test(item)) {
						let [key, val] = item.split('=');
						val = /^\d+$/.test(val) ? parseFloat(val) : val;
						if (paramsObj.hasOwnProperty(key)) {
							paramsObj[key] = [].concat(paramsObj[key], val);
						} else {
							paramsObj[key] = val;
						}
					} else {
						paramsObj[param] = true;
					}
					return paramsObj;
				})
				this.$store.commit('setCompanyCode', paramsObj.companyCode)
				uni.switchTab({
					url: '../home/home'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
.main-page {
  height: 100%;
  background-color: $color-white;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.loading {
  width: 100rpx;
  height: 40rpx;
  margin: 0 auto;
  // margin-top:100rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.loading {
  width: 100rpx;
  height: 40rpx;
  margin: 0 auto;
  // margin-top:100rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.loading text {
  display: inline-block;
  width: 10rpx;
  height: 100%;
  border-radius: 8rpx;
  background: #bf375a;
  -webkit-animation: load 1s ease infinite;
  opacity: 0.5;
}

@-webkit-keyframes load {
  0%,
  100% {
    height: 40rpx;
    background: #bf375a;
  }

  50% {
    height: 70rpx;
    margin: -15rpx 0;
    background: #bf375a;
    opacity: 1;
  }
}

.loading text:nth-child(2) {
  -webkit-animation-delay: 0.2s;
}

.loading text:nth-child(3) {
  -webkit-animation-delay: 0.4s;
}

.loading text:nth-child(4) {
  -webkit-animation-delay: 0.6s;
}

.loading text:nth-child(5) {
  -webkit-animation-delay: 0.8s;
}

.loadingText {
  margin-top: 30px;
}
</style>
